package com.daoxiang.controller;

import com.daoxiang.pojo.ShoppingCart;
import com.daoxiang.pojo.ShoppingCartCustom;
import com.daoxiang.service.ShoppingCartCustomService;
import com.daoxiang.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

@Controller
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartCustomService shoppingCartCustomService;

    @PostMapping("/ajaxAddShoppingCart")
    public ResponseEntity<String> ajaxAddShoppingCart(ShoppingCart shoppingCart) {
        ShoppingCart sc = this.shoppingCartService.queryShoppingCartByUidAndGid(shoppingCart);

        if (sc != null) {
            shoppingCart.setNum(shoppingCart.getNum()+1);
            int i = this.shoppingCartService.updateShoppingCart(shoppingCart);
        } else {
            int i = this.shoppingCartService.insertShoppingCart(shoppingCart);
        }
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    @GetMapping("/ajaxQueryShoppingCart")
    public ResponseEntity<List<ShoppingCartCustom>> ajaxQueryShoppingCart(ShoppingCart shoppingCart) {
        List<ShoppingCartCustom> shoppingCartCustomList = this.shoppingCartCustomService.queryShoppingCart(shoppingCart);
        return new ResponseEntity<List<ShoppingCartCustom>>(shoppingCartCustomList, HttpStatus.OK);
    }

    @PutMapping("/ajaxUpdateShoppingCart")
    public ResponseEntity<String> ajaxUpdateShoppingCart(ShoppingCart shoppingCart) {
        int i = this.shoppingCartService.updateShoppingCart(shoppingCart);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    @DeleteMapping("/ajaxDeleteShoppingCart")
    public ResponseEntity<String>  ajaxDeleteShoppingCart(ShoppingCart shoppingCart) {
        int i = this.shoppingCartService.deleteShoppingCart(shoppingCart);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }
}
